import { HashMap } from "../../api/struct/HashMap";
import { E_LordGlobal } from "../defs/lord/E_LordGlobal";
import { E_TrainPos } from "../defs/train/E_TrainPos";
import { CDLordGlobal } from "../excel/CDLordGlobal";
import { CDLordTraining } from "../excel/CDLordTraining";
import { Train } from "../struct/Train";

/**
 * 数据-训练
 */
export abstract class SDTrain {

    /**
    * 最高等级
    * @param lv 
    * @returns 
    */
    public static isTopLevel(lv: number): boolean {
        return lv === this.getMaxLevel(lv, 1);
    }


    /**
     * 可训练的最高等级
     * @param lv 
     * @param times 
     */
    public static getMaxLevel(lv: number, times: number): number {
        const cks = CDLordTraining.datas.getKeys();
        const maxLevel = cks[cks.length - 1];
        let targetLevel = lv + times;
        if (targetLevel > maxLevel) {
            targetLevel = maxLevel;
        }
        return targetLevel;
    }


    /**
     * 最大共鸣
     * @returns 
     */
    public static getResonateMax(): number {
        const configV = CDLordGlobal.getData(E_LordGlobal.ResonateLevel).value || 10;
        const cks = CDLordTraining.datas.getKeys();
        const maxLevel = cks[cks.length - 1];
        const lv = Math.floor(maxLevel / configV);
        return lv;
    }


    /**
     * 获取需要的道具数量
     * @param lv 
     * @param tlv 
     * @returns 
     */
    public static getLevelupNeedItem(lv: number, tlv: number): HashMap<number, number> {
        const needItems = new HashMap<number, number>();
        for (let i = lv; i < tlv; i++) {
            const c = CDLordTraining.getData(i);
            if (c.costNum > 0) { //如果 <=0 就是最高等级了
                const oldv = needItems.get(c.costItem) || 0;
                needItems.put(c.costItem, oldv + c.costNum);
            }
        }
        return needItems;
    }


    /**
     * 当前的共鸣等级进度
     * @param train 
     * @returns 
     */
    public static getCurResonateProcess(train: Train): number {
        let minv = 9999999;
        for (let i = 0; i < E_TrainPos.EnumCount; i++) {
            const lv = train.getLevel(i);
            if (lv < minv) {
                minv = lv;
            }
        }
        return minv;
    }


    /**
     * 最大共鸣等级进度
     * @param train 
     * @returns 
     */
    public static getMaxResonateLevel(train: Train): number {
        const configV = CDLordGlobal.getData(E_LordGlobal.ResonateLevel).value || 10;
        const maxv = Math.min(1 + train.resonate, this.getResonateMax());
        const rv = configV * maxv;
        return rv;
    }


}
